#include "dialog.h"
#include "ui_dialog.h"
#include <QtCore>

#include "libMediaCapture.h"
#include "libMediaPusher.h"
#include "libEasyPlayerAPI.h"


static Dialog* gThis = NULL;

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    m_bStartRec = false;
    gThis = this;
    m_pPushHandle = NULL;
    m_pCapHandle = NULL;
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::save_bmp( char *pData, int nLen, const char *strName)
{
    BITMAPFILEHEADER bfh;
    memset(&bfh, 0, sizeof(bfh));
    bfh.bfType = 'MB';
    bfh.bfSize = sizeof(bfh) + nLen + sizeof(BITMAPINFOHEADER);
    bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

    BITMAPINFOHEADER bih;
    memset(&bih, 0, sizeof(bih));
    bih.biSize = sizeof(bih);
    bih.biWidth = 640;
    bih.biHeight = 480;
    bih.biPlanes = 1;
    bih.biBitCount = 24;
    bih.biCompression = BI_RGB;

    FILE* bmpFile;
    if (bmpFile = fopen(strName,"wb"))
    {
        fwrite(&bfh, 1, sizeof(bfh), bmpFile);
        fwrite(&bih, 1, sizeof(bih), bmpFile);
        fwrite(pData, 1, nLen, bmpFile);
        fclose(bmpFile);
    }
}

void MyMediaCapture_CB2(int nType, char* pBuff, int nBuffLen, bool bKeyFrame)
{
    if (gThis->m_pPushHandle)
    {
        MediaPusher_PushCapData(gThis->m_pPushHandle, nType, pBuff, nBuffLen, bKeyFrame);
    }
}

void MyRawCapture_CB(int nType, char* pBuff, int nBuffLen, bool bKeyFrame)
{
    static QSharedMemory* pSharedMem = new QSharedMemory("TamiRobot_Camera_Live");
    static int nCount = 0;
    static bool bFirst = true;
    if( bFirst ){
        pSharedMem->create(1920*1080*3);
        bFirst = false;
    }
    nCount += 1;
    if( nCount > 6 ){ // 共享帧率控制
        nCount = 0;
    }
    if( nCount == 1 ){
        pSharedMem->lock();
        int* pData = (int*)pSharedMem->data();
        pData[0] = 640;
        pData[1] = 480;
        memcpy((char*)pData+2*sizeof(int), pBuff, nBuffLen);
        qDebug()<<"Copy Mem:"<<pData[0]<<pData[1]<<nBuffLen;
        pSharedMem->unlock();
    }
}

int __EasyPusher_Callback(int _id, EASY_PUSH_STATE_T _state, EASY_AV_Frame *_frame, void *_userptr)
{
    if (_state == EASY_PUSH_STATE_CONNECTING)               qDebug()<<("Connecting...");
    else if (_state == EASY_PUSH_STATE_CONNECTED)           qDebug()<<("Connected");
    else if (_state == EASY_PUSH_STATE_CONNECT_FAILED)      qDebug()<<("Connect failed");
    else if (_state == EASY_PUSH_STATE_CONNECT_ABORT)       qDebug()<<("Connect abort");
    //else if (_state == EASY_PUSH_STATE_PUSHING)             TRACE("P->\n");
    else if (_state == EASY_PUSH_STATE_DISCONNECTED)        qDebug()<<("Disconnect.");

    return 0;
}
void Dialog::on_pushButton_clicked()
{
    m_pCapHandle = MediaCapture_Create();
    m_pPushHandle = MediaPusher_Create();

    MediaCaptureVideoInfo vInfo;
    vInfo.nIndex = 0;
    vInfo.nWidth = 640;
    vInfo.nHeight = 480;
    vInfo.nFPS = 15;
    MediaCaptureAudioInfo aInfo;
    aInfo.nIndex = 0;
    aInfo.nChannels = 2;
    aInfo.nSamplesPerSec = 44100; // 不支持16000
    aInfo.nBitsPerSample = 16;
    MediaCapture_Init(m_pCapHandle, &vInfo, &aInfo);

    //MediaCapture_SetCB(m_pCapHandle, MyMediaCapture_SaveFileCB, MyMediaCapture_SaveFileCB, false);
    qDebug()<<"Create Handle:"<<m_pCapHandle<<m_pPushHandle;

}

void Dialog::on_pushButton_2_clicked()
{
    MediaCapture_GetSPSAndPPS(m_pCapHandle, m_sps, m_spslen, m_pps, m_ppslen);
    MediaPusher_SetSPSAndPPS(m_pPushHandle, m_sps, m_spslen, m_pps, m_ppslen);

    qDebug()<<"MediaPusher_SetSPSAndPPS";

    MediaPusher_Init(m_pPushHandle, "192.168.112.65", 554, "tt1.sdp", __EasyPusher_Callback);
    MediaCapture_SetCB(m_pCapHandle, MyMediaCapture_CB2, MyMediaCapture_CB2, false);
    MediaCapture_SetCB(m_pCapHandle, MyRawCapture_CB, NULL, true);
}

void Dialog::on_pushButton_5_clicked()
{
    EasyPlayer_Init();
}

void Dialog::on_pushButton_6_clicked()
{
    HWND hWnd=(HWND)ui->lab_video->winId();
    m_nPlayerChannel = EasyPlayer_OpenStream("rtsp://192.168.112.65:554/tt1.sdp", hWnd, DISPLAY_FORMAT_RGB24_GDI,
                                             1, "admin", "admin", NULL, NULL);
    EasyPlayer_SetShownToScale(m_nPlayerChannel, 0);

    EasyPlayer_SetFrameCache(m_nPlayerChannel, 3); //设置缓存
    EasyPlayer_PlaySound(m_nPlayerChannel);

    //    EasyPlayer_CloseStream(m_nPlayerChannel);
    //    EasyPlayer_Release();
}

void Dialog::on_pushButton_7_clicked()
{
    static QSharedMemory* pCurMem = new QSharedMemory("TamiRobot_Camera_Live");
    if( !pCurMem->isAttached() ){
        pCurMem->attach();
    }
    pCurMem->lock();
    int* pData = (int*)pCurMem->data();
    int nWidth = pData[0];
    int nHeight = pData[1];
    int nBuffLen = nWidth*nHeight*3;
    qDebug()<<"Mem Pic:"<<nWidth<<nHeight<<nBuffLen;
    static char* pOut = new char[1920*1080*3];
    memcpy(pOut, (char*)pData+2*sizeof(int), nBuffLen);
    pCurMem->unlock();

    save_bmp(pOut, nBuffLen,"mem_out.bmp"); // RGB相反，上下颠倒
    // =================

    QImage curImg((uchar*)pOut, nWidth, nHeight, QImage::Format_RGB888);
    curImg.save("qimg.jpg");

}
